/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.trandio.dao.ConfigDAO;
import org.trandio.dao.ConfigInterface;
import org.trandio.dao.LiquidacionDAO;
import org.trandio.dao.LiquidacionInterface;
import org.trandio.model.ConfigVO;

/**
 *
 * @author root
 */
public class SetCabecera extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    private LiquidacionInterface liquidacionInterface;    
    private ConfigInterface configInterface;
    private ConfigVO configVO;

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int idcab = 0;
            liquidacionInterface = new LiquidacionDAO();
            configInterface = new ConfigDAO();
            String numero_factura_real = "";
            
            HttpSession session = request.getSession(true);
            int idPrestador = (Integer) session.getAttribute("idprestadorsede") ;
            int idSede   = (Integer) session.getAttribute("idsede");
            
            
            
            

            try {
                
                /*generar numero factura*/
                configVO = configInterface.traerConfig(idPrestador);
                
                int numero_factura = configVO.getCons_actfact()+1;


                System.out.println("Numero:"+numero_factura+"------"+configVO.getCons_finfact());

                if(numero_factura<=configVO.getCons_finfact()){
                    liquidacionInterface.actualizarConsFact(numero_factura);
                    numero_factura_real = configVO.getPrefijo_fact()+numero_factura;
                }else{
                    numero_factura_real = "ERROR-"+numero_factura;
                }


                System.out.println("NumeroCal:"+numero_factura_real);
                
                                
                Date fecha = new Date();    
                SimpleDateFormat formatoDeFecha = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");            
                String fecFac = formatoDeFecha.format(fecha);                                                 
                
                
                if(request.getParameter("fecha")!="")                                
                    fecFac = request.getParameter("fecha");
                                

                idcab = liquidacionInterface.setCabecera(request.getParameter("idpaciente"), idSede, request.getParameter("identidad").trim(), request.getParameter("idcontrato"), request.getParameter("idservicio"), request.getParameter("iddetalleturno"), request.getParameter("estado"), request.getParameter("impresa"), request.getParameter("idfunc"),numero_factura_real, fecFac);
                
                
                int idCita = 0;
                if(request.getParameter("idCita")!=""){
                    idCita = Integer.parseInt(request.getParameter("idCita"));
                }
                
                
                liquidacionInterface.actualizarCitaFactura(idcab,idCita);
                
                
            } catch (SQLException ex) {
                Logger.getLogger(SetCabecera.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NamingException ex) {
                Logger.getLogger(SetCabecera.class.getName()).log(Level.SEVERE, null, ex);
            }


            out.println(idcab);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

